import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button } from "antd";
import * as Apis from '../../apis/info';
import {Text} from '../../components/util/text';
const text = new Text();

const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export class rewardDialogStore {

    //@observable tableStore = new TableStore();

    @observable columns = [];
    @observable datas = [];
    @observable selectedDatas = [];         //选中项受控

    @observable status = {
        visible: false,     //加分dialog的visible
    }

    type = "";      //type表示当前操作方式： 1:列表上方加分按钮点击。2:从每行记录中点击加分。3:信息处理卡片页面中点击加分按钮触发。 4：列表中快捷加分。 5：卡片中快捷加分
    parent = null;
    constructor(parent) {
        this.parent = parent;
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    //初始化加分项tableStore
    @action
    getRewardList = (params = {}) => {
        this.datas = [];
        this.selectedDatas = [];
        this.type = params.type;
        let newParams = {...params };
        Apis.getRewardList(newParams).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.setState({ visible: true })
                this.columns = data.columns;
                this.datas = data.datas;
                //快捷操作加分时默认赋值
                if(this.type == '4' || this.type == '5') {
                    this.selectedDatas = this.datas;
                }
            } else {
                message.error(data.api_errormsg);
            }
        });
    }

    //关闭弹出框,清空数据
    close = (params = {}) => {
        this.datas=[];
        this.selectedDatas=[];
        this.setState({ visible: false });
        if(this.type == '2' || this.type == '3') {
            //展示加分历史记录
            this.parent.setState({ scoreVisible: true });
        }
    }

    //保存选择的加分项
    saveReward = (params = {}) => {
        let flag = true;        //表单校验
        this.selectedDatas && this.selectedDatas.map(m => {
            delete m.cascaderJson;
            if (m.type == '1' && m.values[0] == '') {
                flag = false;
            }
            if (m.type == '2') {
                if (m.values[0] == '' || m.values[1] == '') {
                    flag = false;
                }
            }
        });
        if (flag) {
            //快捷加分时不需要校验是否勾选 
            if ( this.selectedDatas.length == 0) {
                message.warn(text.CHOOSESCORE);
                return;
            }
            let newParams = { 'tableJson': JSON.stringify(this.selectedDatas), 'ids': this.parent.ids };
            Apis.saveAddScore(newParams).then(result => {
                if (result.api_status == '1') {
                    if (!result.isRight) {
                        message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                        return;
                    }
                    if (typeof (result.msg) != 'undefined') {
                        message.error(result.msg);
                        return;
                    }
                    //更新待编分数信息和历史分数记录
                    if(this.type == '1' || this.type == '2' || this.type == '4') {
                        let table = this.parent.tableStore;
                        table.getDatas(table.dataKey, table.current, table.pageSize, table.sortParams);
                    }
                    if(this.type == '2' || this.type == '3') {
                        let table = this.parent.scoreTableStore;
                        table.getDatas(table.dataKey, table.current, table.pageSize, table.sortParams);
                        //展示加分历史记录
                        this.parent.setState({ scoreVisible: true });
                    }
                    if(this.type == '3' || this.type == '5') {
                        if (window.opener && window.opener._table && window.opener._table.reLoad) {
                            window.opener._table.reLoad();
                        }
                    }
                    this.datas = [];
                    this.selectedDatas = [];
                    this.setState({ visible: false });
                } else {
                    message.error(result.api_errormsg);
                }
            })
        } else {
            message.warn(text.NODATA);
        }
    }
}